package com.gzw.scm.base;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import com.gzw.scm.ConfigureLog4J;
import com.gzw.scm.MyApplication;
import com.gzw.scm.activity.LoginActivity;
import com.gzw.scm.bean.CodeInfo;
import com.gzw.scm.bean.Constant;
import com.gzw.scm.receiver.NetBroadcastReceiver;
import com.gzw.scm.utils.JsonUtil;
import com.gzw.scm.utils.NetUtils;
import com.gzw.scm.utils.T;

/**
 * Created by Administrator on 2017/8/7.
 */

public abstract class NewBaseActivity extends AppCompatActivity implements NetBroadcastReceiver.NetEvevt{
    public static NetBroadcastReceiver.NetEvevt evevt;
    /**
     * 网络类型
     */
    private int netMobile;
    private Context mContext;

    private String INTENT_ACTION = "com.symbol.scanconfig.SCANDEMO";
    private String DATA_STRING_TAG = "com.symbol.scanconfig.decode_data";
    private String INTENT_CATEGORY = "com.symbol.category.DEFAULT";

//    private String INTENT_ACTION = "com.symbol.mybroadcast";
//    private String DATA_STRING_TAG = "com.symbol.datawedge.data_string";
//    private String INTENT_CATEGORY = "com.symbol.category.DEFAULT";

    public static final String SCN_CUST_ACTION_SCODE = "com.android.server.scannerservice.broadcast";
    public static final String SCN_CUST_EX_SCODE = "scannerdata";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (MyApplication.getFlag() == -1) {
//            protectApp();
        } else {

            setContentView();

            //加载配置
            ConfigureLog4J configureLog4J=new ConfigureLog4J();
            configureLog4J.configure();

            initViews();
            initData();
            initEvents();
            evevt = this;
            inspectNet();
            isConnect();
//            initBroadcastReceiver();
            mContext=this;
        }
    }

    protected abstract void setContentView();

    protected abstract void initViews();

    protected abstract void initData();

    protected abstract void initEvents();

    protected void protectApp() {
        Intent intent = new Intent();
        intent.setClass(this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(Constant.KEY_PROTECT_APP, true);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    protected void exitApp(){
        finish();
    }


    protected boolean isNull(EditText et) {
        boolean b = false;
        if (isNull(et.getText().toString())) {
            b = true;
        }
        return b;
    }

    protected boolean isNull(TextView tv) {
        boolean b = false;
        if (isNull(tv.getText().toString())) {
            b = true;
        }
        return b;
    }

    protected boolean isNull(String str) {
        boolean b = false;
        if (str.trim() == null
                || str.trim().length() == 0) {
            b = true;
        }
        return b;
    }

    /**
     * 含有标题和内容和一个按钮的对话框
     **/
    protected AlertDialog showAlertDialog(String title, String message, DialogInterface.OnClickListener onClickListener) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
                .setMessage(message)
                .setPositiveButton("确定", null).show();
        return alertDialog;
    }

    /**
     * 含有标题和内容和一个按钮的对话框
     **/
    protected AlertDialog showAlertDialog(String message) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("提示")
                .setMessage(message)
                .setPositiveButton("确定", null).show();
        return alertDialog;
    }

    /**
     * 含有标题、内容、两个按钮的对话框
     **/
    protected AlertDialog showAlertDialog(String title, String message,
                                          String positiveText,
                                          DialogInterface.OnClickListener onPositiveClickListener,
                                          String negativeText,
                                          DialogInterface.OnClickListener onNegativeClickListener) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
                .setMessage(message)
                .setPositiveButton(positiveText, onPositiveClickListener)
                .setNegativeButton(negativeText, onNegativeClickListener)
                .show();
        return alertDialog;
    }

    /**
     * 含有标题、内容、图标、两个按钮的对话框
     **/
    protected AlertDialog showAlertDialog(String title, String message,
                                          int icon, String positiveText,
                                          DialogInterface.OnClickListener onPositiveClickListener,
                                          String negativeText,
                                          DialogInterface.OnClickListener onNegativeClickListener) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
                .setMessage(message).setIcon(icon)
                .setPositiveButton(positiveText, onPositiveClickListener)
                .setNegativeButton(negativeText, onNegativeClickListener)
                .show();
        return alertDialog;
    }

    /**
     * 初始化时判断有没有网络
     */

    public boolean inspectNet() {
        this.netMobile = NetUtils.getNetWorkState(this);
        return isNetConnect();
    }

    /**
     * 网络变化之后的类型
     */
    @Override
    public void onNetChange(int netMobile) {
        // TODO Auto-generated method stub
        this.netMobile = netMobile;
        isNetConnect();

    }

    /**
     * 判断有无网络 。
     *
     * @return true 有网, false 没有网络.
     */
    public boolean isNetConnect() {
        if (netMobile == 1) {
            return true;
        } else if (netMobile == 0) {
            return true;
        } else if (netMobile == -1) {
            return false;

        }
        return false;
    }

    private void isConnect(){
        if(NetUtils.isConnected(this)==false){
            T.showShort(this,"当前网络不可用，请检查网络设置");
        }
    }


    protected String scanRelust(String code){
        return code;
    };

    private String getcode(String msg){
        String code = null;
        if(msg.contains("SSID")){
            try {
                CodeInfo codeInfo = JsonUtil.getPerson(msg, CodeInfo.class);
                code = codeInfo.getNO();
            }catch (Exception e){
                code="该二维码不是周转箱";
            }
        }else{
            code = "该二维码不是周转箱";
        }
        return code;
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        return super.onKeyUp(keyCode, event);
    }

    protected void onResume() {

        IntentFilter filter = new IntentFilter();
        filter.addAction(INTENT_ACTION);
        filter.addCategory(INTENT_CATEGORY);
        registerReceiver(receiver, filter);
        super.onResume();
    }

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(INTENT_ACTION)) {
                String data = intent.getStringExtra(DATA_STRING_TAG);
                scanRelust(data);
            }

        }
    };

    protected void onPause() {

        unregisterReceiver(receiver);
        super.onPause();
    }

}

